@using SixLabors.ImageSharp
@using SixLabors.ImageSharp.Formats
@using SixLabors.ImageSharp.Processing
@inject IStringLocalizer<Visitors> L
<MudCard Style="width:215px">
    <MudCardMedia @bind-Image="@ImageUrl" Height="200" />
    <MudCardContent>
        <MudText Typo="Typo.body2">@Desciption</MudText>
    </MudCardContent>
    <MudCardActions>
        <InputFile id="@id" OnChange="UploadPhoto" hidden accept=".jpg, .jpeg, .png" />
        <MudButton HtmlTag="label" for="@id" Disabled="@Disabled" Variant="Variant.Text" Color="MudBlazor.Color.Primary">@Label</MudButton>
    </MudCardActions>
</MudCard>

@code {
    [EditorRequired]
    [Parameter]
    public string? ImageUrl { get; set; }
    [EditorRequired]
    [Parameter]
    public EventCallback<string?> OnUploaded { get; set; }    
    [EditorRequired]
    [Parameter]
    public string? Label { get; set; }
    [EditorRequired]
    [Parameter]
    public string? Desciption { get; set; }
    [EditorRequired]
    [Parameter]    
    public string? Tag { get; set; }
    [EditorRequired]
    [Parameter]
    public string? PassCode { get; set; }
    [EditorRequired]
    [Parameter]
    public bool Disabled { get; set; }
    private string id = Guid.NewGuid().ToString();
    [Inject] private IUploadService _uploadService { get; set; } = default!;

    private async Task UploadPhoto(InputFileChangeEventArgs e)
    {
        var file = e.File;
        var filestream = file.OpenReadStream();
        var imgstream = new MemoryStream();
        await filestream.CopyToAsync(imgstream);
        imgstream.Position = 0;
        using (var outStream = new MemoryStream())
        {
            using (var image = Image.Load(imgstream))
            {
                var scale = 0m;
                if (image.Width > 1000)
                {
                    scale = 0.3m;
                }
                else if(image.Width <=1000 && image.Width>500)
                {
                    scale = 0.5m;
                }
                else
                {
                    scale = 1m;
                }
                image.Mutate(i => i.AutoOrient()
                                   .Resize(Convert.ToInt32((image.Width*scale)), 0)
                                   .Resize(new ResizeOptions() { Mode = SixLabors.ImageSharp.Processing.ResizeMode.Crop, Size = new SixLabors.ImageSharp.Size(350, 350) }));
                image.Save(outStream, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder());
                var filename = file.Name;
                var fi = new FileInfo(filename);
                var ext = fi.Extension;
                var result = await _uploadService.UploadAsync(new UploadRequest()
                    {
                        Data = outStream.ToArray(),
                        FileName = Tag + "_" + Guid.NewGuid().ToString() + ".jpg",
                        Extension = ext,
                        Folder = PassCode,
                        UploadType = UploadType.VisitorPricture
                    });
                ImageUrl = result.Replace("\\", "/");
                await OnUploaded.InvokeAsync(ImageUrl);
            }
        }

        Snackbar.Add(L["Upload attachment successfully"], MudBlazor.Severity.Info);

    }
}
